//
//  BCWalletDetailNormalCell.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/12/12.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "BCWalletDetailNormalCell.h"

@implementation BCWalletDetailNormalCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.fd_enforceFrameLayout = true;
        [self setNormalViews];
    }
    return self;
}
- (void)setNormalViews {
    self.titleLabel = [QMUILabel new];
    self.titleLabel.textColor = [UIColor qmui_colorWithHexString:@"#666666"];
    self.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    self.titleLabel.numberOfLines = 0;
    [self.contentView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView).offset(BCWalletDetailCellContentLeftPadding);
        make.top.mas_equalTo(self.contentView).offset(BCWalletDetailCellContentTopPadding);
        make.width.mas_lessThanOrEqualTo(self.contentView).offset(-2*BCWalletDetailCellContentLeftPadding);
    }];
    //
    CGFloat viewHeight = [HomeCellDataView defaultHeight]-5.0;
    self.timeView = [HomeCellDataView new];
    self.timeView.dataLabel.textColor = [UIColor qmui_colorWithHexString:@"#333333"];
    self.timeView.dataLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightRegular];
    [self.contentView addSubview:self.timeView];
    [self.timeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(BCWalletDetailCellTitleViewPadding);
        make.height.mas_equalTo(viewHeight);
        make.left.mas_equalTo(self.titleLabel);
        make.right.mas_equalTo(self.contentView.mas_centerX);
    }];
    //
    self.numberView = [HomeCellDataView new];
    self.numberView.dataLabel.textColor = [UIColor qmui_colorWithHexString:@"#333333"];
    self.numberView.dataLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightRegular];
    [self.contentView addSubview:self.numberView];
    [self.numberView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.timeView);
        make.left.mas_equalTo(self.timeView.mas_right).offset(BCWalletDetailCellContentLeftPadding);
        make.right.mas_equalTo(self.contentView).offset(-BCWalletDetailCellContentLeftPadding);
        make.height.mas_equalTo(viewHeight);
    }];
}

/**
 自计算titleSize时 给定的fitsSize
 */
- (CGSize)titieFitsSize:(CGSize)size {
    return CGSizeMake(size.width-2*BCWalletDetailCellContentLeftPadding,CGFLOAT_MAX);
}

- (CGSize)sizeThatFits:(CGSize)size {
    CGFloat height = 0;
    height += BCWalletDetailCellContentTopPadding;
    //
    CGSize titleFitsSize = [self titieFitsSize:size];
    height += [self.titleLabel sizeThatFits:titleFitsSize].height;
    //
    height += BCWalletDetailCellTitleViewPadding;
    //
    height += ([HomeCellDataView defaultHeight]-5.0);
    //
    height += BCWalletDetailCellContentBottomPadding;
    return CGSizeMake(size.width, height);
}

- (void)setCellData:(UserWalletDetailsDataModel *)cellData {
    [super setCellData:cellData];
    //
    NSString *title = cellData.title?:@"";
    NSString *info = cellData.info?:@"";
    NSString *titleString = [title stringByAppendingString:info];
    NSMutableAttributedString *titleAttri = [[NSMutableAttributedString alloc] initWithString:titleString];
    //
    NSRange titleRange = [titleString rangeOfString:title];
    if (titleRange.location!=NSNotFound) {
        if (cellData.status==UserWalletDetailsDataModelStatusReduce) {
            [titleAttri addAttributes:@{NSForegroundColorAttributeName:[UIColor darkSubMainColor]} range:titleRange];
        }else {
            [titleAttri addAttributes:@{NSForegroundColorAttributeName:[UIColor darkMainColor]} range:titleRange];
        }
    }
    self.titleLabel.attributedText = titleAttri;
    
    //
    self.timeView.title = cellData.timeCaption;
    //
    NSDate *timeDate = [NSDate stringToDate:cellData.time format:FullDateFormateString];
    if (timeDate) {
        self.timeView.data = [timeDate stringWithFormat:@"HH:mm MM/dd"];
    }else {
        self.timeView.data = cellData.time;
    }
    //
    self.numberView.title = [cellData.numberCaption stringByReplacingOccurrencesOfString:@"{}" withString:cellData.curr];
    //判断增加或减少
    NSString *changeMoney = cellData.change_money;
    if (cellData.status==UserWalletDetailsDataModelStatusIncrease) {//+
        changeMoney = [@"+" stringByAppendingString:changeMoney];
    }else if (cellData.status==UserWalletDetailsDataModelStatusReduce) {//-
        changeMoney = [@"-" stringByAppendingString:changeMoney];
    }
    self.numberView.data = changeMoney;
    
    //
    
}


@end
